package com.jp.knowledge.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.ImageView;

import com.google.gson.JsonObject;
import com.jiayen.util.ToasUtil;
import com.jp.knowledge.comm.InfoDetailData;
import com.jp.knowledge.R;
import com.jp.knowledge.http.HttpHelper;
import com.jp.knowledge.model.IModel;
import com.jp.knowledge.model.ReplyListModel;
import com.jp.knowledge.my.base.RecycleVAdapter;
import com.jp.knowledge.my.base.RecycleVHolder;
import com.jp.knowledge.util.ImageLoadUtil;
import com.jp.knowledge.util.ResultSubscriber;
import com.jp.knowledge.util.TimeUtil;

import java.util.List;

/**
 * Created by 528787067@qq.com on 2017/1/14.
 */

public class InfoTopicDetailAdapter extends RecycleVAdapter<ReplyListModel> implements View.OnClickListener, ResultSubscriber.OnResultListener {

    private static final int ITEM_PRAISE_CODE = 0x01;
    private InfoDetailData infoDetailData;
    private String uuid;

    public InfoTopicDetailAdapter(Context context, @NonNull List<ReplyListModel> list, String uuid) {
        super(context, list);
        this.uuid = uuid;
        infoDetailData = InfoDetailData.getInfoDetail();
    }

    @Override
    public int getLayoutId(int viewType) {
        return R.layout.item_info_topic;
    }

    @Override
    public void onBind(RecycleVHolder holder, int position) {
        ReplyListModel replyInfo = getData(position);

        ImageLoadUtil.loadCircle(mContext,
                replyInfo.getPortrait() + "?imageView2/2/w/80/interlace/1",
                holder.getImageView(R.id.item_topic_info_head));
        holder.setTextView(R.id.item_topic_info_name, replyInfo.getUserName());
        holder.setTextView(R.id.item_topic_info_praise_text, replyInfo.getPraiseNum()+"");
        holder.setTextView(R.id.item_topic_info_comment_text, replyInfo.getReplyNum()+"");
        ImageView praiseImg = holder.getImageView(R.id.item_topic_info_praise_img);
        praiseImg.setTag(position);
        if(replyInfo.getIsPraise() == 1) {
            praiseImg.setImageResource(R.mipmap.give_the_thumbs_up_lan);
            holder.getTextView(R.id.item_topic_info_praise_text)
                    .setTextColor(mContext.getResources().getColor(R.color.font_orign));
        } else {
            praiseImg.setImageResource(R.mipmap.give_the_thumbs_up);
            holder.getTextView(R.id.item_topic_info_praise_text)
                    .setTextColor(mContext.getResources().getColor(R.color.font_gray_think));
        }
        praiseImg.setOnClickListener(this);
        holder.setTextView(R.id.item_topic_info_time, TimeUtil.friendly_time(replyInfo.getTime()));

        String content = replyInfo.getContent();
        if(content == null)
            content = "";
        ReplyListModel.ReplyDataBean replyDataBean = replyInfo.getReplyData();
        if(replyDataBean != null){
            if(replyDataBean.getUserName() != null || replyDataBean.getContent() != null)
                content += "//";
            if(replyDataBean.getUserName() != null)
                content += ("@" + replyDataBean.getUserName());
            if(replyDataBean.getContent() != null) {
                if(replyDataBean.getUserName() != null)
                    content += "：";
                content += replyDataBean.getContent();
            }
        }
//        if(replyInfo.getUid().equals(uuid)){
//            holder.get(R.id.item_topic_info_delete).setVisibility(View.VISIBLE);
//        } else{
//            holder.get(R.id.item_topic_info_delete).setVisibility(View.GONE);
//        }
        holder.setTextView(R.id.item_topic_info_content, content);
        holder.get(R.id.item_topic_info_reply_view).setVisibility(View.GONE);
    }

    @Override
    public void onClick(View v) {
        if(v.getId() != R.id.item_topic_info_praise_img)
            return;
        int position = (int) v.getTag();
        if(mList.get(position).getIsPraise() == 1){
            ToasUtil.toast(mContext, "请勿重复点赞");
            return;
        }
        ReplyListModel replyInfo = getData(position);
        JsonObject param = new JsonObject();
        param.addProperty("token", HttpHelper.token);
        param.addProperty("topicId", infoDetailData.getTopicInfo().getTopicId());
        param.addProperty("infoId", infoDetailData.getLinkId());
        param.addProperty("type", infoDetailData.getLinkType());
        param.addProperty("commentId", replyInfo.getCommentId());
        HttpHelper.getInstance(mContext).topicPraise(param, position*10+ITEM_PRAISE_CODE, this);
    }

    @Override
    public void onStart(int requestType) {}
    @Override
    public void onCompleted(int requestType) {}
    @Override
    public void onError(int requestType) {
//        if(requestType%10 == ITEM_PRAISE_CODE)
//            ToasUtil.toast(mContext, "抱歉，评论点赞失败");
    }
    @Override
    public void onNext(IModel data, int requestType) {
        if(data.getErrcode() != 0){
            ToasUtil.toast(mContext, data.getMessage());
            onError(requestType);
            return;
        }
        if(requestType%10 != ITEM_PRAISE_CODE)
            return;
        int position = requestType/10;
        mList.get(position).setIsPraise(1);
        mList.get(position).setPraiseNum(mList.get(position).getPraiseNum()+1);
        notifyDataSetChanged();
    }
}
